package com.smartlock.web;

//@RestController
//@RequestMapping("/lock")
public class BindLockController {

    //    private static ExecutorService executorService = Executors.newFixedThreadPool(3);
    //    private Logger                 logger          = LoggerFactory.getLogger(BindLockController.class);
    //
    //    @ResponseBody
    //    @RequestMapping("/bind/response")
    //    public ApiResult<Boolean> bind(BindResponseParam param) throws IOException, InterruptedException {
    //        logger.info("接收到绑定锁响应请求：" + JSON.toJSONString(param));
    //        if (StringUtils.isBlank(param.getLockMac())) {
    //            throw new IllegalArgumentException(param.getLockMac());
    //        }
    //        byte[] command = BindLockService.getBindCommond(param.getLockMac());
    //        Socket socket = takeSocket(param.getLockMac());
    //        if (socket == null) {
    //            logger.error("锁绑定响应失败，无法获取智能锁连接！lockMac=" + param.getLockMac());
    //            return ApiResult.fail(Boolean.FALSE, "锁绑定响应失败，无法获取智能锁连接！");
    //        }
    //        OutputStream out = socket.getOutputStream();
    //        out.write(command);
    //        out.flush();
    //        logger.info("发送绑定锁响应指令lockMac:" + param.getLockMac() + ",指令内容为：" + BytesUtils.formatHexString(command));
    //        return ApiResult.ok(Boolean.TRUE, "绑定锁响应指令发送成功！");
    //    }
    //
    //    private Socket takeSocket(String lockMac) {
    //        Callable<Socket> socketCallable = () -> {
    //            Socket socket = null;
    //            while (socket == null) {
    //                socket = Main.getMacSocketMap().get(lockMac);
    //            }
    //            return socket;
    //        };
    //        Future socketFuture = executorService.submit(socketCallable);
    //        try {
    //            return (Socket) socketFuture.get(20l, TimeUnit.SECONDS);
    //        } catch (InterruptedException e) {
    //            logger.error("获取锁连接异常：", e);
    //        } catch (ExecutionException e) {
    //            logger.error("获取锁连接异常：", e);
    //        } catch (TimeoutException e) {
    //            logger.error("获取锁连接超时，锁mac=" + lockMac, e);
    //        }
    //        return null;
    //    }
}
